package com.webservice_springboot.config;

import com.webservice_springboot.server.random_int.RandomIntInterface;
import com.webservice_springboot.server.weather.WeatherInterface;
import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import javax.xml.ws.Endpoint;

/**
 * webservice的cxf配置类
 */
@Configuration
public class CxfConfig {
    @Resource
    private WeatherInterface weatherInterface;
    @Resource
    private RandomIntInterface randomIntInterface;

    /**
     * 默认servlet路径/services/*,如果覆写则按照自己定义的来
     * 注入Servlet，注意beanName不能为dispatcherServlet
     * 注意：【在springboot2.0之前，此项在application.yml中配置，配置参数为cxf.path=/services 】
     */
    @Bean
    public ServletRegistrationBean cxfServlet() {
        return new ServletRegistrationBean(new CXFServlet(), "/webservice/*");
    }

    @Bean(name = Bus.DEFAULT_BUS_ID)
    public SpringBus springBus() {
        return new SpringBus();
    }

    /**
     * 配置天气公共服务
     */
    @Bean
    public Endpoint weatherInterfaceEndpoint() {
        EndpointImpl endpoint = new EndpointImpl(springBus(), weatherInterface);
        endpoint.publish("/weather");

        return endpoint;
    }

    /**
     * 配置随机数公共服务
     */
    @Bean
    public Endpoint randomIntInterfaceEndpoint() {
        EndpointImpl endpoint = new EndpointImpl(springBus(), randomIntInterface);
        endpoint.publish("/randomInt");

        return endpoint;
    }
}
